package com.scirillo.frases.util;

import java.util.Calendar;

import com.scirillo.frases.R;
import com.scirillo.frases.db.DataProvider;
import com.scirillo.frases.notification.FrasesNotificationManager;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.util.DisplayMetrics;

public class Util {
	
	private static Util mInstance;
	private static Context mContext;
	public static Util getInstance()
	{
		if(mInstance == null)
			mInstance = new Util();
		return mInstance;
	}
	
	private Util(){
	}
	
	public void setCurrentContext(Context context){
		mContext = context;
	}
	
	public static Context getCurrentContext(){
		return mContext;
	}
	
	public void getCurrentFraseIndex() {
		Cursor cursor = mContext.getContentResolver().query(DataProvider.Frase.CONTENT_URI, null, null, null, null);
		int fraseIndex = 0;
		if(cursor!=null){
			if(cursor.moveToFirst()){
				fraseIndex = cursor.getInt(cursor.getColumnIndex(DataProvider.Frase.INDICE));
			}
			if(fraseIndex!=0){
				Constants.CURRENT_INDEX = fraseIndex;
			}
			cursor.close();	
		}		
	}

	public void saveCurrentFraseIndex() {
		
		ContentValues values = new ContentValues();
		values.put(DataProvider.Frase.INDICE, Constants.CURRENT_INDEX+1);
		values.put(DataProvider.Frase.DAY, getdDayOfYear());
		mContext.getContentResolver().update(DataProvider.Frase.CONTENT_URI, values, null, null);
		
	}

	private int getdDayOfYear(){
		Calendar c = Calendar.getInstance(); 
		int day = c.get(Calendar.DAY_OF_YEAR); 
		return day;
	}
	
	private boolean hasNewFrases(){
		int day = getdDayOfYear();
		Cursor cursor = mContext.getContentResolver().query(DataProvider.Frase.CONTENT_URI, null, null, null, null);
		int daySaved = 0;
		if(cursor!=null){
			if(cursor.moveToFirst()){
				daySaved = cursor.getInt(cursor.getColumnIndex(DataProvider.Frase.DAY));
			}		
			cursor.close();	
		}	
		if(daySaved<day){
			return true;
		}else{
			return false;
		}
	}
	
	public void displayNotification() {
		if(hasNewFrases()){
			FrasesNotificationManager.displaytNotification();
		}		
	}
	
	public static String[] getFrases(){
		return mContext.getResources().getStringArray(R.array.frases);		
	}

	public static boolean isSdPresent()
	{
		return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
	}
	
	public static float convertPixelsToDp(float px, Context context){
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    float dp = px / (metrics.densityDpi / 160f);
	    return dp;
	}
	
	public static float convertDpToPixel(float dp, Context context){
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    float px = dp * (metrics.densityDpi / 160f);
	    return px;
	}
	public static String getBuidVersion(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (Exception e) {
			return "01.01.01";
		}
	}
	
	
	public static String removeExtra(String text){
		String ret = text;
		if(text.contains("#L")){
			ret = text.replaceAll("#L", "");
		}else if(text.contains("#M")){
			ret = text.replaceAll("#M", " ");	
		}else if(text.contains("#F")){
			ret = text.replaceAll("#F", " ");	
		}else if(text.contains("#R")){
			ret = text.replaceAll("#R", " ");	
		}else if(text.contains("#H")){
			ret = text.replaceAll("#H", " ");	
		}else if(text.contains("#A")){
			ret = text.replaceAll("#AM", " ");	
		}else if(text.contains("#AM")){
			ret = text.replaceAll("#AL", " ");	
		}else if(text.contains("#AL")){
			ret = text.replaceAll("#AP", " ");	
		}else if(text.contains("#AP")){
			ret = text.replaceAll("#AP", " ");	
		}
		return ret;
	}

}
